﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using Aspose.Cells;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;



public partial class Report_UserControl_uc_PendingOrdersReport : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private DataTable ConvertToDataTable<prc_Report_TestResult>(List<prc_Report_TestResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(prc_Report_TestResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (prc_Report_TestResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    protected void btnXuat_Click(object sender, EventArgs e)
    {
        OutReport outRPT = new OutReport();
        //lay database
        ReportBO rpt = new ReportBO();
        List<ZD_PRC_PENDING_ORDERSResult> result = new List<ZD_PRC_PENDING_ORDERSResult>();
        result = rpt.Report_PendingOrders();
        DataTable dt1 = ConvertToDataTable(result);
        DataTable dt2 = new DataTable();
        DataColumn column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "DateReport";
        dt2.Columns.Add(column);

        DataRow row = dt2.NewRow();
        row["DateReport"] = DateTime.Now.ToString("dd/MM/yyyy");
        dt2.Rows.Add(row);
        // datatable 1
        DataTable Report1 = new DataTable();
        // datatable 2
        DataTable Report2 = new DataTable();
        Report1 = dt1;
        Report2 = dt2;
        Report1.TableName = "Report";
        Report2.TableName = "Date";
        DataTable[] arrTable = { Report1, Report2 };
        outRPT.OutReportToScreen(MapPath("~/RPTTemplate/RPT_PENDINGORDERS_TEMPLATE.xls"), arrTable, "_PendingOrders.xls");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ReportBO rpt = new ReportBO();
        List<ZD_PRC_PENDING_ORDERSResult> result = new List<ZD_PRC_PENDING_ORDERSResult>();
        result = rpt.Report_PendingOrders();
        grdList.DataSource = result;
        grdList.DataBind();
    }
    protected void grdrList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdList.PageIndex = e.NewPageIndex;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            SortGridView(GridViewSortExpression, ASCENDING);
        }
        else
        {
            SortGridView(GridViewSortExpression, DESCENDING);
        }

    }
    protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void grdList_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void grdList_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        GridViewSortExpression = sortExpression;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING);
        }
    }
    public void SortGridView(string sortExpression, string direction)
    {
        //  You can cache the DataTable for improving performance
        ReportBO rpt = new ReportBO();
        List<ZD_PRC_PENDING_ORDERSResult> result = new List<ZD_PRC_PENDING_ORDERSResult>();
        result = rpt.Report_PendingOrders();
        DataTable dt = ConvertToDataTable(result);
        DataView dv = new DataView(dt);
        if(sortExpression!=null)
            dv.Sort = sortExpression + direction;

        grdList.DataSource = dv;
        grdList.DataBind();

    }
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";

    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];
        }
        set
        {
            ViewState["sortDirection"] = value;
        }
    }

    public string GridViewSortExpression
    {
        get
        {
            if (ViewState["sortExpression"] != null)
                return ViewState["sortExpression"].ToString();
            else return null;
        }
        set
        {
            ViewState["sortExpression"] = value;
        }
    }
}
